﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; 


using ThoiTrang.Business;
using ThoiTrang.Entities;


public partial class Admin_adm_articleFeatured_upload : System.Web.UI.Page
{
    public string ckid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        a_Session ss = null;
        a_User usr = null;
        a_UserType usrType = null;
        if (SessionManager.IsLogged)
        {
            ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
            ckid = ss.CookieID;
            usr = a_UserService.Geta_User(ckid);
            usrType = a_UserTypeService.Geta_UserType(usr.UserTypeID);
        }
        else
        {
            Response.Redirect("~/login.aspx");
        }

        if (!IsPostBack)
        {
            loadData();
        }
    }

    void loadData()
    {
        DataTable dtb = a_ArticleService.Get_articleFeaturedUpload().Tables[0];

        #region load images to upload
        dtlUploadImage.DataSource = dtb;
        dtlUploadImage.DataBind();
        #endregion

        DataTable f1,f2,f3,f4,f5;
        f1 = dtb.Clone(); f2 = dtb.Clone();
        f3 = dtb.Clone(); f4 = dtb.Clone();
        f5 = dtb.Clone();

        try
        {
            f1.ImportRow(dtb.Select("FeaturedType=1")[0]);
        }
        catch { }
        try
        {
            f2.ImportRow(dtb.Select("FeaturedType=2")[0]);
        }
        catch { }
        try
        {
            f3.ImportRow(dtb.Select("FeaturedType=3")[0]);
        }
        catch { }
        try
        {
            f4.ImportRow(dtb.Select("FeaturedType=4")[0]);
        }
        catch { }
        try
        {
            f5.ImportRow(dtb.Select("FeaturedType=5")[0]);
        }
        catch { }

        grvF1.DataSource = f1;
        grvF1.DataBind();
        grvF2.DataSource = f2;
        grvF2.DataBind();
        grvF3.DataSource = f3;
        grvF3.DataBind();
        grvF4.DataSource = f4;
        grvF4.DataBind();
        grvF5.DataSource = f5;
        grvF5.DataBind();
    }

    public string FeaturedImage(object aID, object kTitle, object fYear, object fType)
    {
        string res = "";
        res = kTitle.ToString().ToLower().Trim().Replace(" ", "-");
        res =FOConstants.Dir_ImgArticle+ res+ "-" + fYear.ToString() + "-c"+aID.ToString() + fType.ToString()+".jpg";
        return res;
    }

    protected void btnDelete1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string[] param = e.CommandArgument.ToString().Split(';');
            this.RemoveRealImage(param[1]);
        }
    }
   
    protected void btnDelete2_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string[] param = e.CommandArgument.ToString().Split(';');
            this.RemoveRealImage(param[1]);
        }
    }
    protected void btnDelete3_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string[] param = e.CommandArgument.ToString().Split(';');
            this.RemoveRealImage(param[1]);
        }
    }
    protected void btnDelete4_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string[] param = e.CommandArgument.ToString().Split(';');
            this.RemoveRealImage(param[1]);
        }
    }
    protected void btnDelete5_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string[] param = e.CommandArgument.ToString().Split(';');
            this.RemoveRealImage(param[1]);
        }
    }
    protected void grvF1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grvF2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grvF5_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grvF3_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grvF4_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    public void RemoveRealImage(string relativePath)
    {
        char[] spl = { '/', '\\' };
        string rAdminFolder = Server.MapPath("");
        rAdminFolder=rAdminFolder.Substring(0,rAdminFolder.IndexOf("Admin"));
        try
        {
            System.IO.File.Delete(rAdminFolder.Trim(spl) + "/" + relativePath.Trim(spl));
        }
        catch
        { }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string s = "";
        foreach (DataListItem itm in dtlUploadImage.Items)
        {
            Literal ltr = (Literal)itm.FindControl("ltrValue");
            FileUpload flu = (FileUpload)itm.FindControl("fluFile");
            Literal ltrIP = (Literal)itm.FindControl("ltrImagePath");

            //HTMLHelper.ResizeImageFile
            if (flu.PostedFile != null)
            {
                if (flu.PostedFile.FileName != "")
                {
                    if (flu.PostedFile.ContentLength > 0)
                    {
                        char[] spl = { '/', '\\' };
                        string fileServer = ltrIP.Text.Replace(FOConstants.Dir_ImgArticle, "").Trim(spl);
                        string dirServer = Server.MapPath("~/"+FOConstants.Dir_ImgArticle).Trim(spl);
                        try
                        {
                            System.IO.File.Delete(dirServer + "\\" + fileServer);
                        }
                        catch { }
                       flu.SaveAs( dirServer + "\\"+fileServer);
                    }
                }
            }
        }
        Response.Write(s);
    }
}
